![]() 09/04/2013 at 22:15 • Filed to: help me halp me | ![]() | ![]() |
This is my stupid, stupid VB code in Excel, can you tell me where I accidentalyed? :
Private Sub Main() intNum1 = Val(InputBox("Pleas enter the first operand", "First operand")) intNum2 = Val(InputBox("Pleas enter the Second operand", "Second operand")) strMathOperation = InputBox("+, -, *, or / for the math operation", "Math operation")
Call strAddUserInput(intNum1, intNum2, strMathOperation) SendResult End Sub
Private Function strAddUserInput(intNum1 As Integer, intNum2 As Integer, strMathOperation As String) As String
Select Case strMathOperation Case "+" strAddUserInput = intNum1 + intNum2 Case "-" strAddUserInput = intNum1 - intNum2 Case "*" strAddUserInput = intNum1 * intNum2 Case "/" strAddUserInput = intNum1 / intNum2 Case Else strAddUserInput = "Invalid math operation specified" End Select End Function Private Sub SendResult() MsgBox (Str("intNum1", "strMathOperation", "intNum2" & "strAddUserInput") = vbOKOnly) End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Call Main End Sub
![]() 09/04/2013 at 22:20 |
|
I don't use Excel so I can't copy and paste it into an editor to look at. What is the error you are getting? Looking at the code I don't see anything majorly out of place other than the MsgBox syntax looks a little iffy... but it might just be because I'm use to working with .NET languages.
![]() 09/04/2013 at 22:25 |
|
Bro, do you even lif— use the next line?
I'm no genius, but does private sub let you call a variable before you've defined it? I can't see where you define strAddUserInput before you call it. It looks like you define it afterwards.
![]() 09/04/2013 at 22:26 |
|
The message box is where I'm getting this:
with the (Str( being highlighted, should I not string function it?
![]() 09/04/2013 at 22:29 |
|
Sorry, I do separate it nicely but the copy into kinja did not jive well.
![]() 09/04/2013 at 22:31 |
|
What are you trying to do with this statement:
MsgBox (Str("intNum1", "strMathOperation", "intNum2" & "strAddUserInput") = vbOKOnly)
I think you are doing something other than intended. It looks like you were trying to cast the 3 variables to a single string. If that is the case they should not have commas between them. You are also trying to set the string equal to a result type... I think you wanted that to be the option for the MsgBox.
Try something like this:
MsgBox("Result of " & intNum1 & strMathOperation & intNum2 & " is " & strAddUserInput, vbOkOnly, "Result Output")
![]() 09/04/2013 at 22:37 |
|
Truth be told I really have know idea what I'm doing for the most part in VB just taking this mandatory class. Just trying to do challenge 3 .
I tried your line and got this:
![]() 09/04/2013 at 22:41 |
|
Try this for the entire sub:
Private Sub SendResult()
MsgBox("Result of " & Str(intNum1) & strMathOperation & Str(intNum2) & " is " & strAddUserInput, vbOkOnly, "Result Output")
End Sub
![]() 09/04/2013 at 22:47 |
|
red. red everywhere...
![]() 09/04/2013 at 23:07 |
|
Try not surrounding the MSGBOX and using a new variable. Since I can't debug I have to guess a bit. Replace the contents of the SendResult sub with this:
strOutput = "Result of " & Str(intNum1) & strMathOperation & Str(intNum2) & " is " & strAddUserInput
MsgBox strOutput
Either one should have worked, but there must be a syntax error. Here's a review material:
http://www.excely.com/excel-vba/usin…
![]() 09/04/2013 at 23:14 |
|
Still didn't work, but thanks anyways.
Have a tit for your effort: